home *** CD-ROM | disk | FTP | other *** search
- Path: ub239.dialup.uwa.edu.au!not-for-mail
- From: prye@cyllene.uwa.edu.au (Peter Rye)
- Newsgroups: comp.lang.c
- Subject: Re: problems with a linked list
- Date: 26 Jan 1996 14:40:22 +0800
- Organization: The University of Western Australia
- Message-ID: <4e9t0m$re@ub239.dialup.uwa.edu.au>
- References: <1996Jan25.125329.23499@dcs.warwick.ac.uk>
- NNTP-Posting-Host: ub239.dialup.uwa.edu.au
-
- D.C.Molero@dcs.warwick.ac.uk (Daniel Castillo Molero) writes:
-
- Daniel,
-
- The major problem is in your store_side function.
- Here you pass the function two pointers to struct side (s).
- The pointers are passed by value, therefore the function attempts
- to modify *copies* of the original pointers, which of course doesn't
- work.
-
- You will no doubt also be told that main should be declared to return
- an int, and that you should always check the return values of your
- 'malloc' (s).
-
- I've made a few adjustments and the code below compiles and runs without
- errors.
-
- #include <stdio.h>
- #include <stdlib.h>
-
- struct side
- {
- int a;
- struct side *next;
- };
-
- struct side *i;
- struct side *first_side;
- struct side *tmp;
-
- /* insert a new element in the list: use pointers to pointers */
- void store_side(struct side **i, struct side **first_side) {
- if (*first_side == NULL) {
- *first_side = *i;
- (*first_side)->next = NULL;
- }
- else {
- (*i)->next = *first_side;
- *first_side = *i;
- }
- }
-
- /* Daniel's original store_side routine below: */
- /* >void store_side(struct side *i, struct side *first_side) { */
- /* > if (first_side == NULL) { first_side = i; first_side->next = NULL; }*/
- /* > else { i->next = first_side; first_side = i; } */
- /* >} */
-
-
- int main(void) { /* NOT void main(void) */
-
- /* insert first element */
- first_side = NULL;
-
- if((i = malloc(1 * sizeof(struct side))) == NULL) { /* check malloc OK */
- printf("malloc for \"i\" failed.\n");
- exit(EXIT_FAILURE);
- }
-
- (i->a) = 0;
- store_side(&i, &first_side); /* pass pointer to pointer */
-
- /* insert second element */
- if((i = malloc(1 * sizeof(struct side))) == NULL) { /* ditto */
- printf("malloc for \"i\" failed.\n");
- exit(EXIT_FAILURE);
- }
-
- (i->a) = 1;
- store_side(&i, &first_side);
-
- /* insert third elemenet */
- if((i = malloc(1 * sizeof(struct side))) == NULL) { /* ditto */
- printf("malloc for \"i\" failed.\n");
- exit(EXIT_FAILURE);
- }
-
- (i->a) = 2;
- store_side(&i, &first_side);
-
- /* print the three elements */
- tmp = first_side;
- printf("side: %d \n",tmp->a);
- while (tmp->next != NULL) {
- tmp = tmp->next;
- printf("side: %d \n",tmp->a);
- }
- return 0; /* return an int */
- }
-
- ub239:~/News/comp/lang/c$ gcc -ansi -pedantic -Wall -o junk test1.c
- ub239:~/News/comp/lang/c$ junk
- side: 2
- side: 1
- side: 0
-
- Hope this helps,
- Peter Rye
- --
- | Peter Rye |
- | Respiratory Research Fellow |
- | Princess Margaret Hospital for Children |
- | Perth, Western Australia |
-